// import _ from 'lodash';
// import urls from './apiUrls';
// import axios from 'axios';
// import qs from 'qs';

/**
 * [request Ajax 公共库]
 * @param  {[String]} apiName   [api 缩写名]
 * @param  {[Object]} paramsObj [Post 参数]
 */
let BW = {
  insertAtCursor(myValue, trueDom) {          // 在光标后插入文本
    if (document.selection) {   // IE 浏览器
        trueDom.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        sel.select();
    } else if (trueDom.selectionStart || trueDom.selectionStart == '0') {   //FireFox、Chrome等
        var startPos = trueDom.selectionStart;
        var endPos = trueDom.selectionEnd;
        // 保存滚动条
        var restoreTop = trueDom.scrollTop;
        trueDom.value = trueDom.value.substring(0, startPos) + myValue + trueDom.value.substring(endPos, trueDom.value.length);
        if (restoreTop > 0) {
            trueDom.scrollTop = restoreTop;
        }
        trueDom.focus();
        trueDom.selectionStart = startPos + myValue.length;
        trueDom.selectionEnd = startPos + myValue.length;
    } else {
        trueDom.value += myValue;
        trueDom.focus();
    }
  },
  evil: (fn) => {
    var Fn = Function;  // 一个变量指向Function，防止有些前端编译工具报错
    return new Fn('return ' + fn)();
  },
  /*
  * url 目标url
  * arg 需要替换的参数名称
  * argVal 替换后的参数的值
  * return url 参数替换后的url
  */
  changeURLArg: (url, arg, argVal) => {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + argVal
    if (url.match(pattern)) {
      let tmp = '/(' + arg + '=)([^&]*)/gi'
      tmp = url.replace(BW.evil(tmp), replaceText)
      return tmp
    } else {
      if (url.match('[?]')) {
        return url + '&' + replaceText
      } else {
        return url + '?' + replaceText;
      }
    }
  },
  // 删除 url 中指定参数,并返回 url
  funcUrlDelL: name => {
    var loca = window.location;
    var baseUrl = loca.origin + loca.pathname + '?';
    var query = loca.search.substr(1);
    if (query.indexOf(name) > -1) {
      var obj = {}
      var arr = query.split('&');
      for (var i = 0; i < arr.length; i++) {
        arr[i] = arr[i].split('=');
        obj[arr[i][0]] = arr[i][1];
      }
      delete obj[name];
      var url = baseUrl + JSON.stringify(obj).replace(/["{}]/g, '').replace(/:/g, '=').replace(/,/g, '&');
      return url
    };
  },
  Toast: (content, isChoke) => {
    wVue.$message({
      message: content,
      duration: isChoke ? 0 : 2000,
      iconClass: 'toast-icon',
      customClass: 'toast-comm'
    });
  },
  sureToast: (content, sureCallback) => {
    wVue.$alert(content, '温馨提示', {
      showCancelButton: true,
      confirmButtonText: '确定',
      type: 'warning',
      callback: (action, instance) => {
        console.log(instance)
        if (typeof(sureCallback) === 'function' && action === 'confirm') sureCallback()
      }
    })
  }
}

export default BW;
